package com.yl.immodule.widget

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.drawable.Drawable
import android.graphics.drawable.LayerDrawable
import android.os.Build
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.ProgressBar
import androidx.annotation.RequiresApi
import com.yl.immodule.R

/**
 * 仿微信相机的拍照/录像按钮
 */
class CaptureButton @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private val outerCircle: ImageView
    private val innerCircle: View
    private val progressBar: ProgressBar
    private var isRecording = false
    private var currentProgress = 0
    private var progressAnimator: ValueAnimator? = null
    private var previousTargetProgress = 0

    init {
        LayoutInflater.from(context).inflate(R.layout.view_capture_button, this, true)
        outerCircle = findViewById(R.id.iv_outer_circle)
        innerCircle = findViewById(R.id.view_inner_circle)
        progressBar = findViewById(R.id.progress_recording)
        
        // 初始状态
        progressBar.visibility = View.GONE
        progressBar.progress = 0
        
        // 确保进度条动画启用
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            progressBar.min = 0
        }
        progressBar.max = 100
    }

    /**
     * 开始录像时显示进度条
     */
    @RequiresApi(Build.VERSION_CODES.N)
    fun startRecording() {
        isRecording = true
        currentProgress = 0
        previousTargetProgress = 0
        progressBar.progress = 0
        progressBar.visibility = View.VISIBLE
        
        // 内圆缩小动画
        innerCircle.animate()
            .scaleX(0.7f)
            .scaleY(0.7f)
            .setDuration(200)
            .start()
    }

    /**
     * 停止录像时隐藏进度条
     */
    fun stopRecording() {
        isRecording = false
        progressBar.visibility = View.GONE
        progressBar.progress = 0
        currentProgress = 0
        previousTargetProgress = 0
        
        // 取消进行中的动画
        progressAnimator?.cancel()
        progressAnimator = null
        
        // 内圆恢复动画
        innerCircle.animate()
            .scaleX(1.0f)
            .scaleY(1.0f)
            .setDuration(200)
            .start()
    }

    /**
     * 更新录像进度，使用动画平滑过渡
     * @param progress 进度值(0-100)
     */
    @RequiresApi(Build.VERSION_CODES.N)
    fun updateProgress(progress: Int) {
        if (!isRecording) return
        
        // 如果目标进度与上一次目标进度相差太小，直接设置进度而不动画
        if (Math.abs(progress - previousTargetProgress) < 2) {
            progressBar.setProgress(progress, true)
            currentProgress = progress
            return
        }
        
        previousTargetProgress = progress
        
        // 取消上一个动画
        progressAnimator?.cancel()
        
        // 创建新的进度动画，从当前值过渡到目标值
        progressAnimator = ValueAnimator.ofInt(currentProgress, progress).apply {
            duration = 16 // 使用较短的动画时间以保持流畅
            interpolator = LinearInterpolator() // 线性插值器确保平滑
            addUpdateListener { animation ->
                val animatedValue = animation.animatedValue as Int
                progressBar.setProgress(animatedValue, true)
                currentProgress = animatedValue
            }
            start()
        }
    }
    
    /**
     * 按下时的动画效果
     */
    fun animatePress() {
        if (!isRecording) {
            innerCircle.animate()
                .scaleX(0.9f)
                .scaleY(0.9f)
                .setDuration(100)
                .start()
        }
    }
    
    /**
     * 释放时的动画效果
     */
    fun animateRelease() {
        if (!isRecording) {
            innerCircle.animate()
                .scaleX(1.0f)
                .scaleY(1.0f)
                .setDuration(100)
                .start()
        }
    }
    
    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        progressAnimator?.cancel()
        progressAnimator = null
    }
} 